﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using HelloMinasan.Business;
using HelloMinasan.Business.Extend;
using HelloMinasan.Domain;
using HelloMinasan.Framework.Web;
using HelloMinasan.UserDefinedType.Dtos;
using HelloMinasan.Web.ViewModels;

namespace HelloMinasan.Web.Song.Controllers
{
    public class ServicesController : ApplicationController
    {
        //LEBSContext db = new LEBSContext();

        private ISongService songService;
        private IUserService userService;
        private IFriendService friendService;

        public ServicesController(ISongService songService, IUserService userService, IFriendService friendService)
        {
            this.songService = songService;
            this.userService = userService;
            this.friendService = friendService;
        }

        //
        // GET: /Services/
        //[AllowAnonymous]
        [HttpGet]
        public ActionResult SuggestWords()
        {
            string query = Request.QueryString["q"];
            StringBuilder sb = new StringBuilder();
            //checking the query parameter sent from view. If it is null we will return null else we will return list based on query.
            if (!string.IsNullOrEmpty(query))
            {
                WebRequest request = WebRequest.Create("http://webapi.hellominasan.com/api/vocabulary/QuickSearch/" + query);
                var response = request.GetResponse().GetResponseStream();
                StreamReader reader = new StreamReader(response);
                List<WordAutoComplete> listItems = new JavaScriptSerializer().Deserialize<List<WordAutoComplete>>(reader.ReadToEnd());
                listItems.Add(new WordAutoComplete() { Word = "Connect failed!" });
                foreach (var item in listItems)
                {
                    sb.Append(item.Word);
                    sb.Append(Environment.NewLine);
                }
            }

            return Json(sb.ToString(), JsonRequestBehavior.AllowGet);
        }

        [Authorize]
        public ActionResult SuggestPerson()
        {
            string query = Request.QueryString["q"];
            StringBuilder sb = new StringBuilder();
            //checking the query parameter sent from view. If it is null we will return null else we will return list based on query.
            if (!string.IsNullOrEmpty(query))
            {
                List<string> listItems = new List<string>();
                //IEnumerable<Relationship> users = db.Relationships
                //    .Where(x => x.User.Username == User.Identity.Name).ToList();

                int totalRecords = 0;
                //IEnumerable<User> users = userService.GetListUser(
                IList<FriendDto> friends = friendService.GetTopFriendsMayKnow(CurrentUser.Id,
                                                     string.Empty, string.Empty, string.Empty,
                                                     1, //friendListModel.CurrentPage,
                                                     5, //friendListModel.PageSize
                                                     out totalRecords, CurrentUser.LangId);

                listItems.Add("admin");
                sb.Append(Environment.NewLine);
                foreach (var item in friends)
                {
                    sb.Append(item.Id);
                    sb.Append(Environment.NewLine);
                }
            }

            return Json(sb.ToString(), JsonRequestBehavior.AllowGet);
        }

        [Authorize]
        public ActionResult SuggestUser()
        {
            string query = Request.QueryString["q"];
            StringBuilder sb = new StringBuilder();
            //checking the query parameter sent from view. If it is null we will return null else we will return list based on query.
            if (!string.IsNullOrEmpty(query))
            {
                //IEnumerable<User> users = db.Users.ToList();
                int totalRecords = 0; 
                //IEnumerable<User> users = userService.GetListUser(
                IList<FriendDto> friends = friendService.GetTopFriendsMayKnow(CurrentUser.Id,
                                                     string.Empty, string.Empty, string.Empty,
                                                     1, //friendListModel.CurrentPage,
                                                     5, //friendListModel.PageSize
                                                     out totalRecords, CurrentUser.LangId);
                foreach (var item in friends)//users)
                {
                    sb.Append(item.Id);
                    sb.Append(Environment.NewLine);
                }
            }

            return Json(sb.ToString(), JsonRequestBehavior.AllowGet);
        }
    }
}
